/*
 * Copyright 2018 - 2050 zyp.All Rights Reserved.
 *
 */
package com.zyp.common.message.common.resolver;

import com.alibaba.fastjson.JSONObject;
import com.zyp.common.message.common.Exception.BusinessException;
import com.zyp.common.message.common.Exception.ExceptionCode;
import com.zyp.common.message.common.Exception.ParamException;
import com.zyp.common.message.common.domain.Result;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.UndeclaredThrowableException;

@Component("handlerExceptionResolver")
public class WlHandlerExceptionResolver implements HandlerExceptionResolver {

	/**
	 * 日志
	 */
	private final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(WlHandlerExceptionResolver.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
										 HttpServletResponse response, Object handler, Exception ex) {

		Result result = null;
		if (ex instanceof BusinessException) {
			BusinessException bex = (BusinessException) ex;
			result = new Result(bex.getErrorCode(), bex.getErrorMsg(), Result.Status.ERROR);
			LOGGER.error("HandlerExceptionResolver catche the Business Exception", ex);
		} else if (ex instanceof NumberFormatException) {
			NumberFormatException bex = (NumberFormatException) ex;
			// 参数异常
			result = new Result(ExceptionCode.SYS_PARAMETERS_VALIDATE_EXCEPTION.code(),
					ExceptionCode.SYS_PARAMETERS_VALIDATE_EXCEPTION.message() + ", " +
							bex.getMessage(), Result.Status.ERROR);
			LOGGER.error("参数异常!",ex);
		} else if (ex instanceof ParamException) {
			ParamException invalidParamException = (ParamException) ex;
			// 参数异常
			result = new Result(ExceptionCode.SYS_PARAMETERS_VALIDATE_EXCEPTION.code(),
					ExceptionCode.SYS_PARAMETERS_VALIDATE_EXCEPTION.message() + ", " +
							invalidParamException.getErrorMsg(), Result.Status.ERROR);
			LOGGER.error("参数异常!"+invalidParamException.getErrorCode(),ex);
		} else if (ex instanceof IllegalArgumentException) {
			IllegalArgumentException illegalArgumentException = (IllegalArgumentException) ex;
			// 参数异常
			result = new Result(ExceptionCode.SYS_PARAMETERS_VALIDATE_EXCEPTION.code(),
					ExceptionCode.SYS_PARAMETERS_VALIDATE_EXCEPTION.message() + ", " +
							illegalArgumentException.getMessage(), Result.Status.ERROR);
			LOGGER.error("业务参数异常!",ex);
		}else if(ex instanceof UndeclaredThrowableException){
			UndeclaredThrowableException e = (UndeclaredThrowableException)ex;
			Throwable e2 = e.getUndeclaredThrowable();
			if(e2 instanceof BusinessException){
				BusinessException bex = (BusinessException) e2;
				result = new Result(bex.getErrorCode(), bex.getErrorMsg(), Result.Status.ERROR);
			}
		}else {	//未捕获到的异常，级别非常严重
			ExceptionCode resultCode = ExceptionCode.SYS_SYSTEM_EXCEPTION;
			result = new Result(resultCode.code(), resultCode.message(), Result.Status.ERROR);
			LOGGER.error("HandlerExceptionResolver catche the System Exception, ", ex);
		}
		
		response.setCharacterEncoding("UTF-8");
		// 返回json格式的数据xx
		try {
			response.setContentType("application/json;charset=UTF-8");
			StringBuffer responseSb = new StringBuffer();
			
			responseSb.append(JSONObject.toJSONString(result));
			
			response.getWriter().println(responseSb.toString());
		} catch (Exception e) {
			LOGGER.error("Response write exception", e);
		}
		return new ModelAndView();
	}

}
